package com.gowhere.controller;


import com.gowhere.service.basedata.OrdersService;
import com.gowhere.service.basedata.ProductService;
import com.gowhere.service.impl.basedata.OrdersServiceImpl;
import com.gowhere.service.impl.basedata.ProductServiceImpl;
import com.gowhere.service.impl.system.RoleServiceImpl;
import com.gowhere.service.impl.system.UserInfoServiceImpl;
import com.gowhere.service.system.RoleService;
import com.gowhere.service.system.UserInfoService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

//自定义一个处理类
public class BaseServlet extends HttpServlet {
    // 定义接口类对象
    public ProductService productService;   // 产品业务层接口对象
    public OrdersService ordersService;
    public RoleService roleService;
    public UserInfoService userInfoService;

    @Override
    public void init() throws ServletException {
        // 通过接口实现类创建接口对象
        productService = new ProductServiceImpl();
        ordersService = new OrdersServiceImpl();
        roleService = new RoleServiceImpl();
        userInfoService= new UserInfoServiceImpl();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");

        String uri = req.getRequestURI();
        //获取方法名
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        //获取执行的目标对象
        //System.out.println(this);
        try {
            //获取执行的方法对象
            Method method =
                    this.getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
            //暴力反射
            method.setAccessible(true);
            //执行目标方法
            method.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
